$(document).ready(function () {

    //初始化footale
    $('.footable').footable();

});



$(function () {
  
    $('#zycsubmit').on('click',function(){ //资源池验证
        var str = $('#zyc #yzmstr').val();
        var resid = $(this).attr('data-id');
        var logtext = $('#zyc #logtext').val();
        var aims = $('#zyc select[name="zxsid"]').val();
        
        var data = {resid:resid,logtext:logtext,aims:aims};
        if(!strCheck.checkEmpty({elem:'#zyc'})) return false;
        $.ajax({
            url:ajaxUrl.checkyzm,
            type: 'post',
            data: {code:str},
            success: function(re){
                if(re.code){
                    layer.msg(re.msg,{icon:1,time:2000,offset :'100px',});
                    sendTransfer(data); //转移资源
                }else{
                    layer.msg(re.msg,{icon:5,time:2000,offset :'100px',});
                }
                // 刷新验证码
                $('#zyc #yzmImg').attr('src',ajaxUrl.captcha_src+Math.random());
                
            },
            error:function(re){
                console.log(re.msg);
            }
        });
    });

    

    $('.transfer').on('click',function(){
        var resid = $(this).attr('data-id');
        var zxsid = $(this).attr('zxsid');
        $("#zyc select option").css('display','block');
        $("#zyc select option").each(function(){
            if($(this).val()==zxsid)$(this).css('display','none');
        });
        layer.open({
                type    : 1,
                title   : false,
                shade   : false,
                offset  : '100px',
                content : $('#zyc'),
                end     : function(){
                    $('#zyc select[name="zxsid"]').val('-1'); //重置选项
                    $('#zyc #logtext, #zyc #yzmstr').val('');//清空文本内容
                }
            });
        $('#zycsubmit').attr('data-id',resid);
    });


    function sendTransfer(data){  //发送更新数据
        
        $.ajax({
            url:ajaxUrl.transfer,
            type: 'post',
            data: data,
            success: function(re){
                layer.closeAll();
                if(re.code){
                    layer.msg(re.msg,{icon:1,offset :'100px',});
                    
                    window.location.reload();
                }else{
                    layer.confirm(re.msg, {icon: 3, title:'转移被拒绝',offset :'100px',btn: ['去修改资源状态', '取消']}, function(index){
                        var resid = re.data.resid;
                        parent.window.location.href= ajaxUrl.update+resid;
                        
                    layer.close(index);
                    });
                }
                
            },
            error:function(re){
                console.log(re.msg);
            }
        });
    }


    //签约跳转  选择签约操作
    $('.signing').click(function () {

        var sid = $(this).attr('data-id');
        var url = $(this).attr('href');
        layer.confirm('新学员签约请点击[新建签约]<br/>重复资源签约请点击[绑定到已有记录]', {
            btn: ['新建签约', '绑定到已有记录', '取消'] //可以无限个按钮
                ,
            btn3: function (index, layero) {
                layer.close(index);
            },
            title:'签约',

            offset: '100px',
            shade: false,
        }, function (index, layero) {
            if(parent.window == window.top){
                window.location.href = url;
            } else {
                parent.window.location.href = url;
            }
        }, function (index, layero) {
            var url = '/admin/signing/bindenroll.html?sid=' + sid;
            if(parent.window == window.top){
                window.location.href = url;
            } else {
                parent.window.location.href = url;
            }  
        });

        return false;
    });


    // 转移资源按钮吸附提示
    var zytips;
    $('.transfer').on('mouseover', function () {
        var that = this;
        zytips = layer.tips('资源转移', that); //在元素的事件回调体中，follow直接赋予this即可
    }).on('mouseout', function () {
        layer.close(zytips);
    });
    
    // 转移资源按钮吸附提示 end


    // 签约按钮吸附提示
    $('.signing').on('mouseover', function () {
        var that = this;
        zytips = layer.tips('签约', that); //在元素的事件回调体中，follow直接赋予this即可
    }).on('mouseout', function () {
        layer.close(zytips);
    });
    
    // 签约按钮吸附提示 end


    //编辑信息父级跳转
    $('.editInfo').click(function(){
        var url = $(this).attr('href');
        parent.window.location.href = url;
        return false;
    });

    //状态的切换按钮的初始化样式
    // var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
    // elems.forEach(function (html) {
    //     var switchery = new Switchery(html, {
    //         size: 'small'
    //     });
    // });


    parent.window.layer.closeAll();  //关闭父级所有加载层

});

